//
//  WYMediaAddressController.m
//  CirclePush
//
//  Created by admin on 2018/8/4.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMediaAddressController.h"
#import "WYMdeiaCell.h"
#import "WYPriceModel.h"
#import "WYPriceRangeCell.h"
#import "WYMediaReusableView.h"
@interface WYMediaAddressController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UIButton *sureButton;
@property (nonatomic,strong)UIButton *cancelButton;
@property (nonatomic,strong)NSMutableArray *addressArray;
@property (nonatomic,strong) NSString *cityName;
@end

@implementation WYMediaAddressController
-(NSMutableArray *)addressArray
{
    if (!_addressArray) {
        _addressArray = [[NSMutableArray alloc]init];
    }
    return _addressArray;
}
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.cityName = @"北京";
    [self setupUI];
    [self loadCityList];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeCity:) name:@"changeCity" object:nil];
    // Do any additional setup after loading the view.
}
-(void)changeCity:(NSNotification *)noc
{
    self.cityName = noc.object[@"city"];
    [self.dataArray removeAllObjects];
    [self loadCityList];
}
-(void)loadCityList
{
    WEAKSELF
    [WYHttpServer getCityListWithState:@"" WithSearchName:self.cityName SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            weakSelf.dataArray = [WYCity getCityListFromJson:response[@"result"]];
            WYCity *city = [[WYCity alloc]init];
            city.id =@"0";
            city.name =@"全部";
            
            [weakSelf.dataArray insertObject:city atIndex:0];
            [weakSelf.collectionView reloadData];
        }
        
        
        
    }];
    
    
    
}
-(void)setupUI
{
    
    UIView *backView = [[UIView alloc]init];
    [self.view addSubview:backView];
    backView.frame = self.view.bounds;
    backView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.2];
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-50)/4, 40);
    //横向最小距离
    layout.minimumInteritemSpacing=10.0f;
    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    [layout setHeaderReferenceSize:CGSizeMake(kScreenWidth, 44)];
    
    UICollectionView * collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 400) collectionViewLayout:layout];
    self.collectionView = collection;
    [backView addSubview:self.collectionView ];
    collection.backgroundColor =[UIColor whiteColor];
    collection.delegate = self;
    collection.dataSource = self;
    
    [collection registerNib:[UINib nibWithNibName:@"WYMediaReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WYMediaReusableView"];
    
    [collection registerNib:[UINib nibWithNibName:@"WYMdeiaCell" bundle:nil] forCellWithReuseIdentifier:@"WYMdeiaCell"];
    [collection registerNib:[UINib nibWithNibName:@"WYPriceRangeCell" bundle:nil] forCellWithReuseIdentifier:@"WYPriceRangeCell"];
    
    [backView addSubview:self.sureButton];
    [backView addSubview:self.cancelButton];
    
}
#pragma mark - uicollectionViewDatasoure

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYMdeiaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYMdeiaCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYMdeiaCell" owner:nil options:nil].firstObject;
    }
    [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
    [cell setCornerRadius:5.0];
    WYCity *city =self.dataArray[indexPath.row];
    cell.textLabel.text =city.name;
    
    if (city.isState) {
        [cell setBorderWithWidth:kLineHeight andColor:RGBA(250, 165, 122, 1.0)];
        [cell setBackgroundColor:RGBA(255, 247, 244, 1.0)];
        //attr.state = NO;
    }else{
        [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
        [cell setBackgroundColor:[UIColor whiteColor]];
        // attr.state = YES;
    }
    return cell;
}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader)
    {
        
        WYMediaReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WYMediaReusableView" forIndexPath:indexPath];
        if (indexPath.section==0) {
            headerView.textLabel.text=@"选择地区";
        }else{
           // headerView.textLabel.text=@"自定义价格";
        }
        reusableview = headerView;
    }
    reusableview.backgroundColor = RGBA(234, 234, 234, 234);
    return reusableview;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        for (WYCity *city in self.dataArray) {
            city.isState = NO;
        }
        [self.addressArray removeAllObjects];
//        for (NSUInteger i = self.collArray.count; i>0; i--) {
//            if ([self.collArray[i-1] isKindOfClass:[WYCity class]]) {
//                [self.collArray removeObjectAtIndex:i-1];
//            }
//        }
        
    }else{
        
        WYCity *city = self.dataArray[0];
        city.isState = NO;
        
//        for (NSUInteger i = self.collArray.count; i>0; i--) {
//            if ([self.collArray[i-1] isKindOfClass:[WYCity class]]) {
//                WYCity *city = self.collArray[i-1];
//                if ([city.name isEqualToString:@"全部"]) {
//                    [self.collArray removeObjectAtIndex:i-1];
//                }
//            }
//        }
        
    }
    WYCity *city =self.dataArray[indexPath.row];
    city.isState = !city.isState;
    if (city.isState) {
        [self.addressArray addObject:city];
    }else{
        [self.addressArray removeObject:city];
    }
    [collectionView reloadData];
    NSLog(@"点击了某一个cell");
}
-(UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [[UIButton alloc]init];
        _sureButton.backgroundColor = RGBA(59, 128, 249, 1.0);
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        _sureButton.frame = CGRectMake(kScreenWidth/2, self.view.height/3*2-50, kScreenWidth/2, 50);
        [_sureButton addTarget:self action:@selector(sureAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    
    
    return _sureButton;
}
-(UIButton *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]init];
        _cancelButton.backgroundColor =RGBA(235, 239, 245, 1.0);
        [_cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancelButton setTitle:@"重置" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:RGBA(51, 51, 51, 1.0) forState:(UIControlStateNormal)];
        _cancelButton.frame =CGRectMake(0, self.view.height/3*2-50, kScreenWidth/2, 50);
        [_cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _cancelButton;
}
-(void)sureAction
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"address" object:@{@"address":self.addressArray}];
    [self.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeShow" object:nil];
}
-(void)cancelAction
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addressCancel" object:nil];
    [self.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeShow" object:nil];
}
@end
